package org.op.editor.doc;

import java.util.Iterator;
import java.util.List;

import javax.swing.JEditorPane;
import javax.swing.text.Document;

import org.op.FrameView;
import org.op.editor.EditorComponentView;
import org.op.service.config.ConfigurationService;
import org.op.service.doc.DocumentConversionService;
import org.op.service.log.LoggerService;
import org.op.util.MessageController;

public class DocumentReloadControllerImpl implements DocumentReloadController {

	private EditorComponentView editorView;
	private DocumentConversionService docConverter;
	private ConfigurationService configService;
	private MessageController messageController;
	private FrameView frameView;
	private LoggerService logger;
	
	public void reloadAllDocuments() {
		try {
			Iterator<JEditorPane> editors = editorView.getEditorPanes().values().iterator();
			while (editors.hasNext()) {
				JEditorPane editor = editors.next();
				Document doc = editor.getDocument();
				doc = reloadDocument(doc);
				editor.setDocument(doc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void reloadCurrentDocument() {

		try {
			Document doc = editorView.getCurrentPane().getDocument();
			doc = reloadDocument(doc);
			editorView.getCurrentPane().setDocument(doc);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private Document reloadDocument(Document doc) throws Exception {
		List<String> content = docConverter.documentToText(doc);
		
		// since editor is bound to the document, this should refresh
		doc = docConverter.textToDocument(content);
		return doc;
	}

	/**
	 *  This needs to be very fast in order to respond to a every key event (on space or enter)
	 *  
	 *  Be careful with this!
	 * 
	 */
	public void reloadCurrentLine() {
		
		try {
			int cursorPoint = editorView.getCurrentPane().getCaretPosition();
			Document doc = editorView.getCurrentPane().getDocument();
			
			// get the line up until the current cursor position
			String line = doc.getText(0, cursorPoint);
			
			if (cursorPoint>0) {
				String lastChar = doc.getText(cursorPoint-1, 1);
				// don't reload if double space
				if (!lastChar.equals(" ")) {
					// check to see if there was a newline in this chunk
					// if so, then only get up to the last newline
					int startPoint = 0;
					String newline = "\n";
					int lineBegin = line.lastIndexOf(newline);
					if (lineBegin >= 0) {
						line = line.substring(lineBegin+1);
						startPoint = lineBegin+1;
					}
					
					// now erase the current document text
					doc.remove(startPoint, line.length());
					
					// reload the line
					docConverter.insertLineIntoDocument(doc, line, startPoint);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public EditorComponentView getEditorView() {
		return editorView;
	}

	public void setEditorView(EditorComponentView editorView) {
		this.editorView = editorView;
	}

	public DocumentConversionService getDocConverter() {
		return docConverter;
	}

	public void setDocConverter(DocumentConversionService docConverter) {
		this.docConverter = docConverter;
	}

	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}

	
	
}
